window.onload = function () {
  // 给页面开头的header
  let head_top = document.querySelector('.header-top');

  //获取轮播部分
  let banner = document.querySelector('.banner .banner-inner');
  let bannerUl = document.querySelector('.banner .banner-inner ul');
  let bannerWidth = banner.offsetWidth;
  let bannerSpans = document.querySelectorAll('.square span');

  // 获取总的新闻列表
  // let newsList = document.querySelectorAll('.news-list');
  // let xert = newsList[0].children;
  // let [xertul, xertli, xerta] = [null, null, null];
  // for (xertul of xert) {
  //   xertli = xertul.children;
  //   for (xerta of xertli) {
  //     xerta.title = xerta.innerText;

  //   }
  // }
  let aAll = document.querySelectorAll('a');
  aAll.forEach(ele => {
    ele.title = ele.innerText;
  })

  // 轮播
  bannerSpans.forEach((ele, index) => {
    ele.onclick = function () {
      bannerSpans.forEach(e => {
        e.classList = "";
      })
      ele.classList.add('current');
      animate(bannerUl, -index * bannerWidth);
    }
  })

  // 给新闻列表的a标签自动添加title属性






  // 给开头的header滚动时添加class,使其fixed
  window.onscroll = function () {
    let scrollVal = window.pageYOffset;
    // console.log(scrollVal);
    if (scrollVal > 50) {
      head_top.classList.add('fixed-header');
    }
    if (scrollVal <= 50) {
      head_top.classList.remove('fixed-header');
    }
  }


  //轮播运动
  function animate(ele, target) {
    clearInterval(ele.timer);
    let speed = ele.offsetLeft < target ? 10 : -10;
    ele.timer = setInterval(function () {
      let result = ele.offsetLeft - target;
      ele.style.left = ele.offsetLeft + speed + "px";
      if (Math.abs(result) < 10) {
        ele.style.left = target + 'px';
        clearInterval(ele.timer);
      }
    }, 15)
  }


}